﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;


namespace wordTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            toolStripStatusLabel1.Text = "Ready.";


        }

        private void letsPrint(string WordDoc)
        {
            // Create an Application object
            Microsoft.Office.Interop.Word.ApplicationClass ac = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Application app = ac.Application;

            // print in the background
            app.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

            // Open the document to print...

            object filename = WordDoc;

            object missingValue = Type.Missing;

            // Setting parameters
            Microsoft.Office.Interop.Word.Document document = app.Documents.OpenOld(ref filename,
            ref missingValue, ref missingValue,
            ref missingValue, ref missingValue, ref missingValue,
                ref missingValue, ref missingValue, ref missingValue, ref missingValue);

            // Print in the background
            object myTrue = true; 
            object myFalse = false;
            app.ActiveDocument.PrintOutOld(ref myTrue, ref myFalse, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref myFalse, ref missingValue, ref missingValue, ref missingValue);
            document.Close(ref missingValue, ref missingValue, ref missingValue);
            while (app.BackgroundPrintingStatus > 0)
            {
                System.Threading.Thread.Sleep(500);
            }

            //Quit application
            app.Quit(ref missingValue, ref missingValue, ref missingValue);


        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.Close();
            //We quit the whole application
            Application.Exit();
        }

        private void druckenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Open File Dialog.
            OpenFileDialog fileDLG = new OpenFileDialog();
            fileDLG.Multiselect = true;
            fileDLG.Title = "Select the documents you like to print?";
            fileDLG.Filter = "Word documents (*.doc;)|*.doc; *.docx; *.rtf |All files (*.*)|*.*";
            // Set the initial directory
            if (Directory.Exists(@"c:\"))
            {
                fileDLG.InitialDirectory = @"c:\";
            }

            fileDLG.ShowDialog();
            toolStripStatusLabel1.Text = "Documents loaded";
            //fill the listview with selected items
            for (int a = 0; a < fileDLG.FileNames.Count(); a++)
            {
                listView1.Items.Add(fileDLG.FileNames[a]);

            }

           
        }

        private void btnAuswahl_Click(object sender, EventArgs e)
        {   // Emtpy (Clear) the list of selected documents
            listView1.Items.Clear();
            toolStripProgressBar1.Value = 0;

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //print the selected items
            toolStripProgressBar1.Maximum = listView1.Items.Count;
            toolStripProgressBar1.Value = 0;
            try
            {
                for (int b = 0; b < listView1.Items.Count; b++)
                {
                    toolStripStatusLabel1.Text = (listView1.Items[b].Text + " is being printed...");
                    Application.DoEvents();
                    letsPrint(listView1.Items[b].Text);
                    toolStripProgressBar1.Value++;
                }
            toolStripStatusLabel1.Text = "Job done.";
            }
            catch (SystemException ex)
            {
                toolStripStatusLabel1.Text = "Error: Word is not installed on your computer";
            }
            finally { }
 
        }

        private void infoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormInfo frm = new FormInfo();
            frm.Show();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            //This if for drag and drop functionality
            string[] strFileName = (string[])e.Data.GetData(DataFormats.FileDrop);
            for (int i = 0; i < strFileName.Count(); i++)
            {
                listView1.Items.Add(strFileName[i]);
            }
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None;
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {   //show a little context menu on right mouse button click
            if(e.Button == MouseButtons.Right)
            entfernen.Show(this, new Point(e.X,e.Y));
        }

        private void toolStripMenuItemEntfernen_Click(object sender, EventArgs e)
        {
            // remove from listview...
            try
            {
                listView1.Items.Remove(listView1.SelectedItems[0]);
            }
            catch { }

        }

    }
}
